package com.guoxue.action.front;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.guoxue.action.common.CheckSwitchIsOpen;
import com.guoxue.action.common.SCSettingStaticName;
import com.guoxue.domain.ClientToServerVo;
import com.guoxue.service.IClientToServerService;
import com.guoxue.util.LoggerUtil;


@Controller
/**
 * 客户端异常捕获和测试切换处理
 * 
 * @author kanghb
 */
public class ClientToServerAction extends BaseAction {
	@Resource
	private IClientToServerService clientToServerService;
	@Resource
	private CheckSwitchIsOpen checkSwitchIsOpen;
	/**
	 * 客户端向服务器发送异常日志
	 * @param req
	 * @param resp
	 * @param jqm 机器码
	 * @param jx  机型
	 * @param sysVersion 系统版本
	 * @param uid 用户id
	 * @param serverInfo 异常日志内容
	 * @param createTime 异常记录时间
	 * @param code 自定义规则
	 * @return
	 */
	@RequestMapping("/sendDebug")
	@ResponseBody
	public JSON sendDebugInfo(HttpServletRequest req, HttpServletResponse resp,String jqm,
			String jx,String sysVersion,Integer uid,String serverInfo,String createTime,String code){
		try{
			String isOpen=checkSwitchIsOpen.IsOpenSwitch(SCSettingStaticName.SWITCH_CLIENT_SENDBUG);
			if("".equals(isOpen)||"no".equals(isOpen)){
				return Message(ERROR, "此功能暂未开启！", null);
			}
			if (jqm==null){
				return Message(ERROR, "未提交机器码", null);
			}
			if (jx==null){
				return Message(ERROR, "未提交机型", null);
			}
			if (sysVersion==null){
				return Message(ERROR, "未提交系统版本", null);
			}
			if (uid==null){
				uid=0;
			}
			if (serverInfo==null){
				return Message(ERROR, "未提交记录的异常内容", null);
			}
			if (createTime==null){
				return Message(ERROR, "未提交记录时间", null);
			}
			if (code==null){
				code="";
			}
			Long time = Long.parseLong(createTime);
			ClientToServerVo sendVo=new ClientToServerVo();
			sendVo.setCode(code);
			sendVo.setIsType(2);
			sendVo.setJqm(jqm);
			sendVo.setJx(jx);
			sendVo.setSysVersion(sysVersion);
			sendVo.setUid(uid);
			sendVo.setServerInfo(serverInfo);
			sendVo.setCreateTime(time);
			clientToServerService.saveClientToServerInfo(sendVo);
			return Message(SUCCESS, "获取信息成功", null);
		}catch(Exception e){
			return LoggerUtil.logInfo(e, "客户端向服务器推送日志错误");
		}
	}
	/**
	 * 发起版本兼容测试请求
	 * @param req
	 * @param resp
	 * @param uid
	 * @return
	 */
	@RequestMapping("/getServerInfo")
	@ResponseBody
	public JSON getServerInfo(HttpServletRequest req, HttpServletResponse resp, Integer uid) {
		try{	
			JSONObject obj = new JSONObject();
			String serverIp="";
			String isOpen=checkSwitchIsOpen.IsOpenSwitch(SCSettingStaticName.SWITCH_CLIENT_ISDEBUG);
			if("".equals(isOpen)||"no".equals(isOpen)){
				return Message(ERROR, "此功能暂未开启！", null);
			}
			if (uid == null) {
				return Message(ERROR, "未提交用户id", null);
			}
			ClientToServerVo getVo=clientToServerService.getclientToServerbyUid(uid,1);
			if(getVo!=null){
				serverIp=getVo.getServerInfo();
			}
			obj.put("serverIp", serverIp);
			return Message(SUCCESS, "获取测试切换成功", obj);
		}catch(Exception e){
			return LoggerUtil.logInfo(e, "获取切换服务器测试失败");
		}
	}

}
